// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import SaveManager from "../../Managers/SaveManager";
import EquipEntity from "../../Models/Equips/EquipEntity";
import EquipManager, { EquipEvent } from "../../Models/Equips/EquipManager";
import BaseUI from "../BaseUI";
import EquipItem from "../UIItems/EquipItem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class EquipmentUI extends BaseUI {
    @property(cc.Prefab)
    equipItemPrefab: cc.Prefab = null;
    @property([EquipItem])
    equipItemArr: EquipItem[] = [];
    @property(cc.ScrollView)
    scrollview: cc.ScrollView = null;

    selectType: number = 0;

    onLoad() {
        this.selectType = 0;
    }

    protected onEnable(): void {

        this.drawEquipBag();
        this.drawWear();
        GameEvent.eventNode.on(EquipEvent.WEAREQUIP_EVENT, this.onUpdataEquip, this);
        GameEvent.eventNode.on(EquipEvent.TAKEOFFEQUIP_EVENT, this.onUpdataEquip, this);
        GameEvent.eventNode.on(EquipEvent.RECYCLEEQUIP_EVENT, this.onUpdataEquip, this);
    }

    protected onDisable(): void {
        GameEvent.eventNode.off(EquipEvent.WEAREQUIP_EVENT, this.onUpdataEquip, this);
        GameEvent.eventNode.off(EquipEvent.TAKEOFFEQUIP_EVENT, this.onUpdataEquip, this);
        GameEvent.eventNode.off(EquipEvent.RECYCLEEQUIP_EVENT, this.onUpdataEquip, this);
    }
    onUpdataEquip() {
        this.drawEquipBag();
        this.drawWear();
    }
    drawWear() {
        var arr = SaveManager.single.userdata.equipWearList;
        for (let i = 0; i < this.equipItemArr.length; i++) {
            const element = this.equipItemArr[i];
            var ey = EquipManager.single.getEquipEntity(arr[i]);
            element.drawItem(ey);
        }
    }

    drawEquipBag() {
        var arr = EquipManager.single.getEquipBagList();
        var nodeArr = this.scrollview.content.children;
        for (const iterator of nodeArr) {
            iterator.active = false;
        }
        var arr2: EquipEntity[] = [];
        for (const iterator of arr) {
            if (iterator.isWear) {
                continue;
            }
            if (this.selectType == 0) {
                arr2.push(iterator);
            } else if (this.selectType == iterator.equipTp.attributeId0) {
                arr2.push(iterator);
            }
        }

        for (let i = 0; i < arr2.length; i++) {
            const element = arr2[i];
            var itemNode: cc.Node = nodeArr[i];
            if (itemNode == null) {
                itemNode = cc.instantiate(this.equipItemPrefab);
                this.scrollview.content.addChild(itemNode);
            }
            itemNode.active = true;
            var script = itemNode.getComponent(EquipItem);
            script.drawItem(element);
        }
    }

    toggleClick(toggle, index: string) {
        switch (index) {
            case "0"://全部
                this.selectType = 0;
                break;
            case "1"://武器+攻击
                this.selectType = 104;//101;
                break;
            case "2"://衣服+生命
                this.selectType = 101;//102;
                break;
            case "3"://手套+暴伤
                this.selectType = 103;
                break;
            case "4"://头盔+暴击
                this.selectType = 102;//104;
                break;
            case "5"://靴子+移速
                this.selectType = 105;
                break;
        }

        this.drawEquipBag();
    }
}
